package com.pbking.facebook.delegates.data
{
	import com.adobe.serialization.json.JSON;
	import com.pbking.facebook.Facebook;
	import com.pbking.facebook.delegates.FacebookDelegate;
	
	/**
	 * delegate for calling data.createObject.  
	 * 
	 * @author sol wu
	 */
	public class CreateObjectDelegate extends FacebookDelegate
	{
		public var objid:Number;
				
		/**
		 * @param properties - name value pair of the data inside the object.  
		 */
		public function CreateObjectDelegate(facebook:Facebook, objtype:String, properties:Object)
		{
			super(facebook);
			if (objtype != "") { fbCall.setRequestArgument("obj_type", objtype); }
			// properties is name-value pair in json format.
			// eg. {"prop1":1,"b":2,"c":3,"d":4,"e":5}
			// in facebook php client, it uses php json_encode() to format an array into it.  
			// eg. fbCall.setRequestArgument("properties", '{"testprop1":"testvalue2","uid":123456789}');
			if (properties) { fbCall.setRequestArgument("properties", JSON.encode(properties));	}
			fbCall.post("facebook.data.createObject");
		}
		
		override protected function handleResult(result:Object):void
		{
			// object id of the created object
			objid = -1;
			// what if it failed?
			if (result) {
				objid = result as Number;
			}
		}
	}
}